package com.by.controller;

import com.by.biz.AirBiz;
import com.by.pojo.Air;
import com.by.util.PageUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class AirController {
    @Autowired
    private AirBiz airBiz;

    /*@RequestParam默认不能为空*/
    @RequestMapping("/getAirByPage")
    public String getAirByPage(Model model, Integer currPage) {
        //第一次访问，当前页是0
        if (currPage == null) {
            currPage = 1;  //就设置为1
        }
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("begin", PageUtil.PAGE_SIZE * (currPage - 1));
        map.put("len", PageUtil.PAGE_SIZE);
        List<Air> list = airBiz.getAirByPage(map);
        int count=airBiz.getTotalCount();
        int totalPage=PageUtil.getTotalPages(count,PageUtil.PAGE_SIZE);
       model.addAttribute("totalPage", totalPage);
       model.addAttribute("count",count);
        model.addAttribute("currPage", currPage);
        model.addAttribute("list", list);
        return "airList";
    }
    @RequestMapping("/addAir")
    public String addAir(Air air){
        int i=airBiz.addAir(air);
        if (i>0){
            return "redirect:getAirByPage.do";
        }else {
            return "error";
        }
    }

    @RequestMapping("/deleteAir")
    public String delete(int id){
        int i=airBiz.deleteAir(id);
        if (i>0){
            return "redirect:getAirByPage.do";
        }else {
            return "error";
        }
    }

}
